package com.module.prms.dao;


import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;

import com.module.prms.vo.FMenuTree;
import com.module.prms.vo.MenuTree;
import com.module.prms.vo.PrmsMenu;

public class MenuDao extends GenericHibernateDao<PrmsMenu, String> {
	
	private MenuDao(){}
	
	private static MenuDao dao =null;
	
	public synchronized static MenuDao getInstance(){
		if(dao == null)
			dao = new MenuDao();
		return dao;
	}

	public FMenuTree getmenutree(Session session)
	{
		String hql= "from PrmsMenu as obj where obj.cmenuPid='0'";
		 Query query = session.createQuery(hql.toString());
		 List<MenuTree> menutree= new ArrayList();
		 List<PrmsMenu> pmenu =query.list();
		 for(PrmsMenu pm:pmenu)
		 {
			 hql="from PrmsMenu as obj where obj.cmenuPid='"+pm.getCmenuId()+"'";
			 query=session.createQuery(hql.toString());
			 List<PrmsMenu> cmenu =query.list();
			 MenuTree mt = new MenuTree();
			 mt.setCcreaterId(pm.getCcreaterId());
			
			 mt.setCmenuId(pm.getCmenuId());
			 mt.setCmenuName(pm.getCmenuName());
			 mt.setCmenuPid(pm.getCmenuPid());
			 mt.setDcreateTime(pm.getDcreateTime());
			 mt.setChildren(cmenu);
			 menutree.add(mt);
		 }
		 FMenuTree fmt = new FMenuTree();
		 fmt.setTree(menutree);
		return fmt;
	}
	
	public FMenuTree getUserMenuTree(Session session,String menuids)
	{
		String hql= "from PrmsMenu as obj where obj.cmenuPid='0'";
		 Query query = session.createQuery(hql.toString());
		 List<MenuTree> menutree= new ArrayList();
		 List<PrmsMenu> pmenu =query.list();
		 for(PrmsMenu pm:pmenu)
		 {
			 hql="from PrmsMenu as obj where obj.cmenuPid='"+pm.getCmenuId()+"' and obj.cmenuId in("+menuids+")";
			 query=session.createQuery(hql.toString());
			 List<PrmsMenu> cmenu =query.list();
			 if(cmenu!=null&&cmenu.size()>0)
			 {
			 MenuTree mt = new MenuTree();
			 mt.setCcreaterId(pm.getCcreaterId());
			
			 mt.setCmenuId(pm.getCmenuId());
			 mt.setCmenuName(pm.getCmenuName());
			 mt.setCmenuPid(pm.getCmenuPid());
			 mt.setDcreateTime(pm.getDcreateTime());
			 mt.setChildren(cmenu);
			 menutree.add(mt);
			 }
		 }
		 FMenuTree fmt = new FMenuTree();
		 fmt.setTree(menutree);
		return fmt;
	}
	
}
